﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.Commons;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.WebUserControls.Frame
{
    public partial class FundingFrame : UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!Page.IsPostBack)
            //{
            //    this.LoadFundingSource();
            //}
        }

        private void LoadFundingSource()
        {
            DropDownListFundingSource.DataSource = DocumentUtilities.FundingSource;
            DropDownListFundingSource.DataBind();
            DropDownListFundingSource.Items.Insert(0, new ListItem("None", string.Empty));
        }

        /// <summary>
        /// Handles the Command event of the ButtonLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonFundingContactLookup_Command(object sender, CommandEventArgs e)
        {
            ContactLookupPopup.Popup();
        }

        /// <summary>
        /// Gets the service funding from form.
        /// </summary>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public ServiceFunding GetServiceFundingDetails()
        {
            var serviceFunding = new ServiceFunding();
            int contactId;
            serviceFunding.FundingContactId = int.TryParse(HiddenFieldFundingContactId.Value, out contactId) ? contactId : 0;

            serviceFunding.FundingSource = DropDownListFundingSource.SelectedValue;
            serviceFunding.FundraisingForText = TextBoxFundraisingForText.Text;
            serviceFunding.FundraisingWhy = TextBoxFundraisingWhy.Text;
            serviceFunding.FundraisingDonationIncremental = CheckBoxDonationIncremental.Checked;
            serviceFunding.FundraisingDonorAnonymous = CheckBoxFundraisingDonorAnonymous.Checked;
            if (CheckBoxFundingContinuationNeeded.Checked)
            {
                serviceFunding.FundingContinuationDetails = TextBoxFundingContinuationDetails.Text;
                serviceFunding.FundingContinuationAmount = double.Parse(TextBoxFundingContinuationAmount.Text, CultureInfo.CurrentCulture);
            }

            // Currency textboxs
            double fundingNeeds, fundingAmount, fundraisingNeeds, donorAmount;
            serviceFunding.FundingAmount = double.TryParse(TextBoxFundingAmount.Text, out fundingAmount) ? fundingAmount : 0;
            serviceFunding.FundingNeeds = double.TryParse(TextBoxFundingNeeds.Text, out fundingNeeds) ? fundingNeeds : (double?)null;
            serviceFunding.FundraisingNeeds = double.TryParse(TextBoxFundraisingNeeds.Text, out fundraisingNeeds) ? fundraisingNeeds : (double?)null;
            serviceFunding.FundraisingDonorAmount = double.TryParse(TextBoxFundraisingDonorAmount.Text, out donorAmount) ? donorAmount : (double?)null;

            // Datetime textboxs
            DateTime fundingStart, fundingEnd, fundraisingRequiredBy, fundraisingDonation, fundraisingCompletedDate;
            serviceFunding.FundingStart = DateTime.TryParse(TextBoxFundingStart.Text, CultureInfo.GetCultureInfo("vi-VN"), DateTimeStyles.None, out fundingStart) ? fundingStart : (DateTime?)null;
            serviceFunding.FundingEnd = DateTime.TryParse(TextBoxFundingEnd.Text, CultureInfo.GetCultureInfo("vi-VN"), DateTimeStyles.None, out fundingEnd) ? fundingEnd : (DateTime?)null;
            serviceFunding.FundraisingRequiredBy = DateTime.TryParse(TextBoxFundraisingRequiredBy.Text, CultureInfo.GetCultureInfo("vi-VN"), DateTimeStyles.None, out fundraisingRequiredBy) ? fundraisingRequiredBy : (DateTime?)null;
            serviceFunding.FundraisingDonationDate = DateTime.TryParse(TextBoxFundraisingDonationDate.Text, CultureInfo.GetCultureInfo("vi-VN"), DateTimeStyles.None, out fundraisingDonation) ? fundraisingDonation : (DateTime?)null;
            serviceFunding.FundraisingCompletedDate = DateTime.TryParse(TextBoxFundraisingCompletedDate.Text, CultureInfo.GetCultureInfo("vi-VN"), DateTimeStyles.None, out fundraisingCompletedDate) ? fundraisingCompletedDate : (DateTime?)null;

            return serviceFunding;
        }

        public void LoadServiceFundingDetails(ServiceFunding serviceFunding)
        {
            if (serviceFunding == null)
            {
                return;
            }

            LoadFundingSource();

            // Dropdownlists
            DropDownListFundingSource.SelectedValue = serviceFunding.FundingSource ?? string.Empty;

            // Textboxs
            TextBoxFundingAmount.Text = serviceFunding.FundingAmount.ToString(CultureInfo.CurrentCulture);
            TextBoxFundraisingDonorAmount.Text = serviceFunding.FundraisingDonorAmount.HasValue
                                                     ? serviceFunding.FundraisingDonorAmount.Value.ToString(CultureInfo.CurrentCulture)
                                                     : string.Empty;
            TextBoxFundingNeeds.Text = serviceFunding.FundingNeeds.HasValue
                                           ? serviceFunding.FundingNeeds.Value.ToString(CultureInfo.CurrentCulture)
                                           : string.Empty;
            TextBoxFundraisingNeeds.Text = serviceFunding.FundraisingNeeds.HasValue
                                               ? serviceFunding.FundraisingNeeds.Value.ToString(
                                                   CultureInfo.CurrentCulture)
                                               : string.Empty;
            TextBoxFundingContinuationAmount.Text = serviceFunding.FundingContinuationAmount.HasValue
                                                        ? serviceFunding.FundingContinuationAmount.Value.ToString(
                                                            CultureInfo.CurrentCulture)
                                                        : string.Empty;

            TextBoxFundingStart.Text = serviceFunding.FundingStart.HasValue ? serviceFunding.FundingStart.Value.ToShortDateString() : string.Empty;
            TextBoxFundingEnd.Text = serviceFunding.FundingEnd.HasValue ? serviceFunding.FundingEnd.Value.ToShortDateString() : string.Empty;
            TextBoxFundraisingRequiredBy.Text = serviceFunding.FundraisingRequiredBy.HasValue ? serviceFunding.FundraisingRequiredBy.Value.ToShortDateString() : string.Empty;
            TextBoxFundraisingCompletedDate.Text = serviceFunding.FundraisingCompletedDate.HasValue ? serviceFunding.FundraisingCompletedDate.Value.ToShortDateString() : string.Empty;
            TextBoxFundraisingDonationDate.Text = serviceFunding.FundraisingDonationDate.HasValue ? serviceFunding.FundraisingDonationDate.Value.ToShortDateString() : string.Empty;

            TextBoxFundraisingForText.Text = serviceFunding.FundraisingForText;
            TextBoxFundraisingWhy.Text = serviceFunding.FundraisingWhy;
            TextBoxFundingContinuationDetails.Text = serviceFunding.FundingContinuationDetails;

            TextBoxFundingContact.Text = string.Format(CultureInfo.CurrentCulture, "{0} {1}",
                                                       serviceFunding.Contact.FirstName, serviceFunding.Contact.Surname);

            // Checkbox
            CheckBoxFundraisingDonorAnonymous.Checked = serviceFunding.FundraisingDonorAnonymous.HasValue && serviceFunding.FundraisingDonorAnonymous.Value;
            CheckBoxFundingContinuationNeeded.Checked = serviceFunding.FundingContinuationAmount != null ||
                                                        serviceFunding.FundingContinuationDetails != null;
            CheckBoxFundraisingComplete.Checked = serviceFunding.FundraisingCompletedDate != null;
            CheckBoxDonationIncremental.Checked = serviceFunding.FundraisingDonationIncremental.Value;

            // Hidden fields
            HiddenFieldFundingContactId.Value = serviceFunding.Contact.ContactId.ToString(CultureInfo.CurrentCulture);

            // Style
            if (CheckBoxFundingContinuationNeeded.Checked)
            {
                TextBoxFundingContinuationAmount.CssClass = "st-forminput funding-continuation-amount";
                TextBoxFundingContinuationDetails.CssClass = "st-forminput funding-continuation-details";

                TextBoxFundingContinuationAmount.Enabled = false;
                TextBoxFundingContinuationDetails.Enabled = false;
            }

            if (CheckBoxFundraisingComplete.Checked)
            {
                TextBoxFundraisingCompletedDate.CssClass = "st-forminput datepicker-input";
                TextBoxFundraisingCompletedDate.Enabled = true;
            }
            
        }
    }
}